package com.pic.mgt.haishin.executer;

import org.springframework.dao.DataAccessException;

import com.pic.mgt.executer.IGXTypeExecutionException;
import com.pic.mgt.haishin.unit.HaishinMessage;
import com.pic.mgt.unit.operation.UnitOperationException;

public final class GeneralHaishinMessageUnitExecuter extends
		HaishinMessageUnitExecuter {

	@Override
	protected void executeIGXType() throws IGXTypeExecutionException {
		HaishinMessage haishinMessage = (HaishinMessage)super.unit;
		
		// 一般配信的訊息只有一種，即是取得 target file解出來的檔案
		
		// 更新資料庫
		try {
			// 新增 target message 資料
			super.insertTargetMessage(haishinMessage);
			// 更新 business target action 的最後交易時間
			super.updateBusinessTargetActionTxTime(haishinMessage);
		} catch (DataAccessException e) {
			throw new IGXTypeExecutionException(
					"Updating general haishin target transaction '" + haishinMessage.getTransactionFilename() +
					"' record to database faild." ,e);
		}
		
		/*
		 *  移除 Message File 
		 *  (包含 Confirmation File,
		 *  	 Confirmation File For Transaction File,
		 *       Transaction File
		 *  三個檔)
		 */
		try {
			// throws UnitOperationException
			super.simpleMessageUnitOperation.removeUnit(haishinMessage);
		} catch (UnitOperationException e) {
			throw new IGXTypeExecutionException(
					"Haishin message '" + haishinMessage.getUnitName() +
					"' execution failed in removing message file process" ,e);
		}
	}
}
